// https://leetcode.cn/problems/reverse-words-in-a-string-iii/

/**
 * @param {string} s
 * @return {string}
 */
// var reverseWords = function(s) {
//   return s.split(' ')
//     .map(s=>s.split('').reverse().join(''))
//     .join(' ');
// };

//理解遍历

export const reverseWords = function (s: string) {
  const word = [];
  let result = "";
  for (let c of s) {
    if (c !== " ") {
      word.push(c);
    } else {
      //revert
      for (let i = word.length - 1; i >= 0; i--) {
        result += word.pop();
      }
      result += c;
    }
  }
  for (let i = word.length - 1; i >= 0; i--) {
    result += word.pop();
  }
  return result;
};
